package com.cspg.ssss.service;

import com.cspg.ssss.domain.CheckBatchInst;
import com.cspg.ssss.domain.CheckConfigTemplate;
import com.cspg.ssss.repository.CheckBatchInstMapper;
import com.cspg.ssss.repository.CheckConfigTemplateMapper;
import com.cspg.ssss.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.Period;
import java.util.*;

@Service
public class CheckBatchService {
    @Autowired
    CheckConfigTemplateMapper checkConfigTemplateMapper;

    @Autowired
    CheckBatchInstMapper checkBatchInstMapper;

    @Autowired
    ProblemGradeService problemGradeService;

    public CheckBatchInst getCurrentCheckBachInst(int check_type){
        CheckBatchInst condition = new CheckBatchInst();
        condition.setCheck_type(check_type);
        return checkBatchInstMapper.selectCurrentBatchInst(condition);
    }

    public int createCheckBatchInst(CheckBatchInst inst){

        List<CheckConfigTemplate> templates =  checkConfigTemplateMapper.selectCheckConfigTemplate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String year = sdf.format(new Date());

        for(int i=0;i<templates.size();i++){
            if(templates.get(i).getCheck_type() == inst.getCheck_type()) {
                String strDay = templates.get(i).getStart_time();
                String strStartDate = year + strDay;
                Date startDate = DateUtil.getWorkDay(strStartDate);

                if (inst.getStart_date().compareTo(startDate) == 0) {
                    //同类型只能存在一个批次为当前批次，在启动新批次前将同类型其他批次全部设置为已完成
                    CheckBatchInst currBatch = getCurrentCheckBachInst(inst.getCheck_type());
                    if(currBatch != null) {
                        //对于已有的同样的批次，则不再重建
                        if(currBatch.getStart_date().compareTo(startDate) == 0){
                            return -2;//批次已存在
                    }
                        currBatch.setCurrent_stage(3);
                        currBatch.setUpdate_time(new Date());
                        updateCheckBatchInst(currBatch);
                  }
                    //启动批次
                    inst.setConfig(templates.get(i).getConfig_content());
                    inst.setCurrent_stage(0);
                    inst.setUpdate_time(new Date());
                    checkBatchInstMapper.insertBatchInst(inst);
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(inst.getStart_date());
                    cal.add(Calendar.MONTH, -6);//减少三个月
                   problemGradeService.syncGradeInfo(inst.getCheck_type(),cal.getTime(),inst.getStart_date());
                    return 0;
              }
            }

       }
        return -1;
    }
    public int createCheckBatchInstsd(CheckBatchInst inst){

        List<CheckConfigTemplate> templates =  checkConfigTemplateMapper.selectCheckConfigTemplate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String year = sdf.format(new Date());

        for(int i=0;i<templates.size();i++){
            if(templates.get(i).getCheck_type() == inst.getCheck_type()) {
                    //同类型只能存在一个批次为当前批次，在启动新批次前将同类型其他批次全部设置为已完成
                    CheckBatchInst currBatch = getCurrentCheckBachInst(inst.getCheck_type());
                    if(currBatch != null) {
                        //对于已有的同样的批次，则不再重建
                            return -2;//批次已存在

                    }
                    //启动批次
                    inst.setConfig(templates.get(i).getConfig_content());
                    inst.setCurrent_stage(0);
                    inst.setUpdate_time(new Date());
                    checkBatchInstMapper.insertBatchInst(inst);
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(inst.getStart_date());
                    cal.add(Calendar.MONTH, -6);//减少三个月
                    problemGradeService.syncGradeInfo(inst.getCheck_type(),cal.getTime(),inst.getStart_date());
                    return 0;
                }
           // }

        }
        return -1;
    }
    public int updateCheckBatchInst(CheckBatchInst inst){
        checkBatchInstMapper.updateBatchInst(inst);
        return 0;
    }

    public CheckBatchInst getCheckBatchById(int id){
        Map<String,Object> condition = new HashMap<String, Object>();
        condition.put("id",id);
        List<CheckBatchInst> checkBatchInsts = checkBatchInstMapper.selectBatchInst(condition);
        if(checkBatchInsts != null && checkBatchInsts.size() == 1){
            return checkBatchInsts.get(0);
        }

        return null;
    }


}
